/**
 * Copyright (c) 2012, SHX Studio.
 * All rights reserved.
 * rosary226@gmail.com
 * 
 * @author shenqianfeng
 * @date 2012-9-1  
 * @version 1.0   
 */


package com.xxx.sqfengine;

import javax.microedition.khronos.opengles.GL10;

import com.xxx.sqfengine.texture.SQFGLTextureManager;
import com.xxx.sqfengine.transform.SQFGLRotateInfo;
import com.xxx.sqfengine.transform.SQFGLScaleInfo;
import com.xxx.sqfengine.transform.SQFGLTranslateInfo;
import com.xxx.sqfengine.transform.SQFGLTransformManager;
import com.xxx.sqfengine.util.SQFGLConstants;

public abstract class SQFGL3DObject {

	protected SQFGLScene mScene;
	
	// mTransformManager is used to manage translate, rotate and scale
	protected SQFGLTransformManager mTransformManager;

	public abstract void draw(GL10 gl);
	
	protected void addToScene() {
		if(null == mScene) {
			SQFGLConstants.logError("mScene should not be null in SQFGL3DObject::addToScene()");
		}
		if(null != mScene) {
			mScene.addObject(this);
		}
	}
	
	
	public SQFGLTransformManager getTransformManager() {
		if(null == mTransformManager) {
			mTransformManager = new SQFGLTransformManager();
		}
		return mTransformManager;
	}

	// getter and setter below
	public SQFGLRotateInfo getRotateInfo() {
		return getTransformManager().getRotateInfo();
	}
	
	public void setRotateInfo(SQFGLRotateInfo info) {
		getTransformManager().setRotateInfo(info);
	}
	
	public SQFGLScaleInfo getScaleInfo() {
		return getTransformManager().getScaleInfo();
	}
	
	public void setScaleInfo(SQFGLScaleInfo info) {
		getTransformManager().setScaleInfo(info);
	}
	
	public SQFGLTranslateInfo getTranslateInfo() {
		return getTransformManager().getTranslateInfo();
	}
	
	public void setTranslateInfo(SQFGLTranslateInfo info) {
		getTransformManager().setTranslateInfo(info);
	}
	
	protected void beginDraw(GL10 gl) {
		gl.glFrontFace(GL10.GL_CCW);
		//gl.glFrontFace(GL10.GL_CW);
		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glCullFace(GL10.GL_BACK);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		gl.glPushMatrix();
	}
	
	protected void endDraw(GL10 gl) {
		gl.glPopMatrix();
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisable(GL10.GL_CULL_FACE);
	}
}
